<?php
/*
* 用户注册：可以搞个腾讯企业邮箱，用企业邮箱来收发邮件，并且关联QQ
 一些系统发送的邮件可以用这个来发送
 用户注册可以自己选定用户名、密码，就会生成一个用户表记录；
 也可以第三方OAuth登录，登录后要求用户起个名字，不强求密码，但是必须OAuth进来5分钟内才可以设置密码；
 邮箱验证之后可以用来重置密码
*/
class MembersController extends MyController
{
    public function init()
    {
        parent::init();
    }
    /*
    * 
    */
    public function actionIndex()
    {

    }

    /*
    * 
    */
    public function actionReg()
    {
        if(Yii::app()->request->isPostRequest)
        {
            
        }
        $this->js = array('jquery.validate.min');
        $this->pageTitle = '新用户注册';
        $this->render('regform');
    }


    /*
    * 
    */
    public function actionLogin()
    {

    }

    /*
    * 
    */
    public function actionOauth_login()
    {
        $auth_channel = Yii::app()->request->getParam('channel');
        $oauth = OAuthMgr::getInstance($auth_channel);
        if(!$oauth)
        {
            //fail
        }
        $this->redirect($oauth->auth_url());
    }

    /*
    * 
    */
    public function actionOauth_callback()
    {
        $auth_channel = trim(strtolower(Yii::app()->request->getParam('channel')));
        $oauth = OAuthMgr::getInstance($auth_channel);
        if(!$oauth)
        {
            //fail
            die('本站暂时不支持这样的登录方式');
        }
        if($oauth->callback_auth())
        {
            //如果已有绑定账户则让其登录
            $binded_acc = Oauth_users::model()->find('auth_channel=:ch and auth_uid=:uid',array(':ch'=>$auth_channel,':uid'=>Yii::app()->session['oauth_uid']));
            if($binded_acc)
            {
                $binded_acc->expire = Yii::app()->session['oauth_expire'];
                $binded_acc->auth_token = Yii::app()->session['oauth_token'];
                $binded_acc->lastlogin_time = date('Y-m-d H:i:s');
                $binded_acc->login_times = 1 + $binded_acc->login_times;
                $binded_acc->save();
                $identity = new IXUserIdentity($binded_acc->user->login_name,'');
                $identity->Oauth_authenticate();
                Yii::app()->user->login($identity,86400*30);
                $this->redirect('/');
            }
            else
            {
                $this->redirect('/members/oauth_bind');
            }
        }
        else
        {
            die('登录失败，请重试。');
        }
    }

    public function actionOauth_bind()
    {
        if(!Yii::app()->session['oauth_uid'])
            $this->redirect('/members/index');
        if($this->_userinfo())
        {
            $user_oauth_record = Oauth_users::model()->find('uid=:uid and auth_channel=:ch',array(':uid'=>$this->_userinfo()->id,':ch'=>Yii::app()->session['auth_channel']));
            if(!$user_oauth_record)
            {
                $user_oauth_record = new Oauth_users;
                $user_oauth_record->uid = $this->_userinfo()->id;
                $user_oauth_record->auth_channel = Yii::app()->session['auth_channel'];
            }
            $user_oauth_record->auth_uid = Yii::app()->session['oauth_uid'];
            $user_oauth_record->expire = Yii::app()->session['oauth_expire'];
            $user_oauth_record->auth_token = Yii::app()->session['oauth_token'];
            $user_oauth_record->lastlogin_time = date('Y-m-d H:i:s');
            $user_oauth_record->login_times = 1;
            $user_oauth_record->save();
            $this->pageTitle = '绑定成功！';
            $this->render('confirm_bind');
        }
        else
        {
            $this->render('set_username');
        }
    }

    public function actionOauth_setname()
    {
        if($this->_userinfo())
        {
            $this->json_return(false,'您已经登陆过了');
        }
        if(!Yii::app()->session['oauth_uid'] || !Yii::app()->session['auth_channel'])
        {
            $this->json_return(false,'您的授权可能已经过期，请重新登录');
        }
        $username = trim(Yii::app()->request->getParam('username'));
        if(!$username)
        {
            $this->json_return(false,'请输入用户名');
        }
        if(Users::model()->find('login_name=:name',array(':name'=>$username)))
        {
            $this->json_return(false,'这个用户名已经存在');
        }
        $password = trim(Yii::app()->request->getParam('password'));
        $oauth = OAuthMgr::getInstance(Yii::app()->session['auth_channel']);
        $oauth->set_access_token(Yii::app()->session['oauth_token']);
        $newuser = new Users;
        $newuser->login_name = $username;
        $newuser->nickname = $oauth->get_remote_nickname();
        $newuser->email = '';
        $newuser->avatar = $oauth->get_remote_avatar();
        $newuser->password = IXCommon::hash_pwd($password);
        $newuser->reg_time = date('Y-m-d H:i:s');
        $newuser->lastlogin_time = date('Y-m-d H:i:s');
        $newuser->save();
        $new_auth_user = new Oauth_users;
        $new_auth_user->auth_channel = Yii::app()->session['auth_channel'];
        $new_auth_user->uid = $newuser->id;
        $new_auth_user->auth_uid = Yii::app()->session['oauth_uid'];
        $new_auth_user->expire = Yii::app()->session['oauth_expire'];
        $new_auth_user->auth_token = Yii::app()->session['oauth_token'];
        $new_auth_user->lastlogin_time = date('Y-m-d H:i:s');
        $new_auth_user->login_times = 1;
        $new_auth_user->save();
        $identity = new IXUserIdentity($username,'');
        $identity->Oauth_authenticate();
        Yii::app()->user->login($identity,86400*30);
        $this->json_return(true,'','/');
    }

    /*
    * 
    */
    public function actionLogout()
    {

    }

    /*
    * 
    */
    public function actionChgpwd()
    {

    }

    /*
    * 
    */
    public function actionResetpwd()
    {

    }

    /*
    * 
    */
    public function actionVerify_email()
    {

    }
}